"""

@Date    ：2025-08-15 19:11 
"""
import random

from django.contrib.auth.hashers import make_password
from django.db.models import Count
from celery import shared_task

from app.models import HostModel, StatisticsModel


@shared_task
def set_host_root_password():
    """
    间隔修改主机root密码
    """
    for item in HostModel.objects.all():
        chars = "abcdefghijklmnopqrstuvwxyz0123456789"
        custom_str = ''.join(random.choice(chars) for _ in range(6))
        item.password = make_password(custom_str)
        item.save()
    return None


@shared_task
def record_host_quantity():
    result = HostModel.objects.values('machine_room_id').annotate(count=Count('id'))
    for item in result:
        StatisticsModel.objects.create(machine_room=item.get('machine_room_id', 0), number=item.get('count', 0))
    return None
